<?php

declare(strict_types=1);


namespace app\controller\mini;

use app\controller\admin\AdminBaseController;
use app\model\admin\Areas;
use app\Request;
use app\model\admin\SysMember;
use app\model\mini\Address;

class AddressController extends AdminBaseController
{
    public function save(Request $request)
    {
        $id =  input('id');
        $params = $request->param();
        $user_id = $request->user_id;

        // // 默认
        if ($params['is_default'] == 1) {
            Address::update([
                "is_default" => 0,
            ], [
                "user_id" => $user_id,
                "is_default" => 1,
            ]);
        }
        // 获取省市区编码
        $p_info = Areas::where([
            "parent_id" => 0,
            "name" => $params['province']
        ])->find();
        $c_info = Areas::where([
            "name" => $params['city']
        ])->where('parent_id', '<>', 0)->find();
        if ($id) {
            // 修改
            Address::update([
                "real_name" => $params['real_name'],
                "phone" => $params['phone'],
                "province" => $params['province'],
                "city" => $params['city'],
                "area" => $params['area'],
                "province_id" => $p_info->area_id,
                "city_id" => $c_info->area_id,
                "is_default" => $params['is_default'],
                "detail" => $params['detail'],
                "lng" => $params['lng'] ?? 0,
                "lat" => $params['lat'] ?? 0,
            ], ["id" => $id, 'user_id' => $user_id]);
        } else {

            Address::create([
                "user_id" => $user_id,
                "real_name" => $params['real_name'],
                "phone" => $params['phone'],
                "province" => $params['province'],
                "city" => $params['city'],
                "area" => $params['area'],
                "province_id" => $p_info->area_id,
                "city_id" => $c_info->area_id,
                "is_default" => $params['is_default'],
                "detail" => $params['detail'],
                "lng" => $params['lng'] ?? 0,
                "lat" => $params['lat'] ?? 0,
            ]);
        }
        return success();
    }
    public function del($id, Request $request)
    {
        $user_id = $request->user_id;
        Address::where([
            "id" => $id,
            "user_id" => $user_id,
        ])->delete();
        return success();
    }
    public function getList(Request $request)
    {
        // 取消分页
        $user_id = $request->user_id;
        // $list = Address::where('user_id', $user_id)
        //     ->order('id', 'desc')->paginate([
        //         'list_rows' => $this->limit,
        //         'page' => $this->page,
        //         'var_page' => $this->pageVar
        //     ]);
        $list = Address::where('user_id', $user_id)
            ->order('id', 'desc')->select();
        return success($list);
    }
    // 设置默认地址
    public function setAddress(Request $request)
    {
        $id = input('id');
        $is_default = input('is_default');
        $user_id = $request->user_id;
        $info = Address::find($id);
        if ($info && $info->user_id == $user_id) {
            // // 已经是默认不修改
            if (!$info->is_default) {
                // 修改原先默认位取消
                Address::update([
                    "is_default" => 0
                ], [
                    "user_id" => $user_id,
                    "is_default" => 1,
                ]);
                $info->is_default = 1;
                $info->save();
            }
        }
        return success();
    }
    public function detail($id, Request $request)
    {
        $info = Address::find($id);
        $user_id = $request->user_id;
        if ($info && $info->user_id == $user_id) {
            return success($info);
        }
        return error();
    }
    public function getInfoByLoca(Request $request)
    {
        $lat = input('lat');
        $lng = input('lng');
        $params = [
            "key" => "QTQBZ-UALKO-FHMWB-SP2VW-KN7V3-GSFUY",
            "location" => $lat . "," . $lng
        ];
        $url = "https://apis.map.qq.com/ws/geocoder/v1/";
        $str = http_build_query($params);
        $res_str = $this->httpRequest($url . "?" . $str, "get");
        $res_arr = json_decode($res_str, true);
        $address = "";
        if ($res_arr['status'] == 0) {
            $address = $res_arr['result'];
        } else {
            return error($res_arr['message']);
        }
        return success($address);
    }
}
